package com.briup.MR.funMR;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.map.TokenCounterMapper;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * yarn jar HdfsClientDemo-1.0-SNAPSHOT.jar com.briup.MR.funMR.TokenCountMR -D input=/user/hdfs/course/course.txt -D output=/user/hdfs/course/token_result
 */
public class TokenCountMR extends Configured implements Tool {
    public int run(String[] strings) throws Exception {
        Configuration conf = getConf();
        String input = conf.get("input");
        String output = conf.get("output");

        Job job = Job.getInstance(conf);
        job.setJarByClass(this.getClass());
        job.setJobName("TokenCountMR");

        /*
        TokenCounterMapper拆分字符串，拆分进入map的值，
        拆分得到的结果为key ，值为1从map中写入
        并且输出键值类型是Text、IntWritable
         */
        job.setMapperClass(TokenCounterMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setNumReduceTasks(0);

        TextInputFormat.addInputPath(job,new Path(input));
        TextOutputFormat.setOutputPath(job,new Path(output));
        return job.waitForCompletion(true)?0:-1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(new ToolRunner().run(new TokenCountMR(),args));
    }
}
